﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics.Contracts;
using ServiceManager.Utils;

namespace QuotationService.RealtimeQuotation
{
    internal class QuotationInfoRepository<TQuotInfo, TQuotInfoEntity>
        where TQuotInfo : Quotation.QuotationInfoBase
        where TQuotInfoEntity : struct
    {
        protected IEntityConverter<TQuotInfo, TQuotInfoEntity> converter;
        public QuotationInfoRepository(IEntityConverter<TQuotInfo, TQuotInfoEntity> converter)
        {
            this.converter = converter;
        }


        /// <summary>
        /// 更新单个商品的行情信息
        /// </summary>
        /// <param name="quotSource"></param>
        /// <param name="quotInfo"></param>
        /// <param name="appendToHistoryList"></param>
        /// <returns></returns>
        public  bool UpdateQuotationInfo(string quotSource, TQuotInfo quotInfo, bool appendToHistoryList)
        {
            Contract.Requires(!string.IsNullOrEmpty(quotSource));
            Contract.Requires(quotInfo != null);
            TQuotInfoEntity quotInfoEntity = this.converter.Convert(quotInfo);
            byte[] bytes = quotInfoEntity.StructToBytes<TQuotInfoEntity>();
            return QuotationInfoChannel.Update(quotSource, quotInfo.StkId.LongCode, bytes, appendToHistoryList);
        }
        /// <summary>
        /// 获取单个商品的行情信息
        /// </summary>
        /// <param name="quotSource"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public TQuotInfo GetQuotationInfo(string quotSource, string code)
        {
            Contract.Requires(!string.IsNullOrEmpty(quotSource));
            Contract.Requires(!string.IsNullOrEmpty(code));
            var bytes = QuotationInfoChannel.Get(quotSource, code);
            if (bytes != null)
            {
                TQuotInfoEntity quotInfoEntity = StructHelper.BytesToStruct<TQuotInfoEntity>(bytes);
                return this.converter.Convert(quotInfoEntity);
            }
            else
                return null;
        }
        /// <summary>
        /// 获取全部商品的行情信息快照
        /// </summary>
        /// <param name="quotSource"></param>
        /// <returns></returns>
        public List<TQuotInfo> GetQuotationInfoSnap(string quotSource)
        {
            Contract.Requires(!string.IsNullOrEmpty(quotSource));
            var allQuotInfoBytes = QuotationInfoChannel.GetAll(quotSource);
            List<TQuotInfo> rtn = new List<TQuotInfo>();
            if (allQuotInfoBytes != null)
            {
                foreach (byte[] bytes in allQuotInfoBytes)
                {
                    TQuotInfoEntity quotInfoEntity = StructHelper.BytesToStruct<TQuotInfoEntity>(bytes);
                    rtn.Add(this.converter.Convert(quotInfoEntity));
                }
            }
            return rtn;
        }
        /// <summary>
        /// 获取单个商品的当日历史行情
        /// </summary>
        /// <param name="quotSource"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public List<TQuotInfo> GetHistoryQuotationInfo(string quotSource, string code)
        {
            Contract.Requires(!string.IsNullOrEmpty(quotSource));
            Contract.Requires(!string.IsNullOrEmpty(code));
            var allQuotInfoBytes = QuotationInfoChannel.GetAll(quotSource, code);
            List<TQuotInfo> rtn = new List<TQuotInfo>();
            if (allQuotInfoBytes != null)
            {
                foreach (byte[] bytes in allQuotInfoBytes)
                {
                    TQuotInfoEntity quotInfoEntity = StructHelper.BytesToStruct<TQuotInfoEntity>(bytes);
                    rtn.Add(this.converter.Convert(quotInfoEntity));
                }
            }
            return rtn;
        }
        /// <summary>
        /// 清除单个商品的行情信息
        /// </summary>
        /// <param name="quotSource"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public  bool ClearQuotionInfo(string quotSource, string code)
        {
            Contract.Requires(!string.IsNullOrEmpty(quotSource));
            Contract.Requires(!string.IsNullOrEmpty(code));
            return QuotationInfoChannel.Clear(quotSource, code);
        }
        /// <summary>
        /// 清除全部商品的行情信息
        /// </summary>
        /// <param name="quotSource"></param>
        /// <returns></returns>
        public  bool ClearAll(string quotSource)
        {
            Contract.Requires(!string.IsNullOrEmpty(quotSource));
            return QuotationInfoChannel.ClearAll(quotSource);
        }
    }
}
